package com.jndv.phoneconfig.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jndv.phoneconfig.DeviceAdapter;
import com.jndv.phoneconfig.PublicConstant;
import com.jndv.phoneconfig.R;
import com.jndv.phoneconfig.command.BluetoothStateCommand;
import com.jndv.phoneconfig.command.DeviceCommand;
import com.jndv.phoneconfig.command.PairResult;
import com.kyleduo.switchbutton.SwitchButton;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class BluetoothFragment extends BaseFragment {

    DeviceAdapter adapter;
    List<PairResult> deviceList =new ArrayList<>();
    List<String> nameList=new ArrayList<>();
    SwitchButton sbBluetooth;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_bluetooth;
    }

    @Override
    protected void initView(View view) {
        sbBluetooth=view.findViewById(R.id.sb_bluetooth);
        sbBluetooth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sendMessage(PublicConstant.BLUETOOTH,isChecked?"1":"0");
                if(!isChecked){
                    deviceList.clear();
                    nameList.clear();
                    adapter.setNewData(deviceList);
                    adapter.notifyDataSetChanged();
                }
            }
        });


        RecyclerView rvLeft=view.findViewById(R.id.rv);
        adapter=new DeviceAdapter(R.layout.item_device_name, deviceList);
        rvLeft.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvLeft.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sendMessage(PublicConstant.PAIR_DEVICE_NAME, deviceList.get(position).deviceName);
            }
        });

        if(isSocketConnect()){
            sendMessage(PublicConstant.CUR_BLUETOOTH_STATE,"");
        }else {
            sbBluetooth.setChecked(false);
            deviceList.clear();
            nameList.clear();
            adapter.setNewData(deviceList);
            adapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurBluetoothStateEvent(BluetoothStateCommand event) {
        sbBluetooth.setChecked(event.isOn);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceEvent(DeviceCommand command){
        if(!nameList.contains(command.name)){
            deviceList.add(new PairResult(command.name,false));
            nameList.add(command.name);
            adapter.setNewData(deviceList);
            adapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPairResultEvent(PairResult result){
        for(PairResult pr: deviceList){
            if(TextUtils.equals(pr.deviceName,result.deviceName)){
                Toast.makeText(getActivity(),"蓝牙配对结果::"+result.isPair,Toast.LENGTH_SHORT).show();
                if(result.isPair){
                    pr.isPair=true;
                }
            }else {
                pr.isPair=false;
            }
        }
        adapter.setNewData(deviceList);
        adapter.notifyDataSetChanged();
    }
}
